require(["jquery","paging","template","bootstrap","echarts","bbcare","scprit/department/view/template/follow-up/common.js"],function($,Paging,template,bootstrap,echarts,$bb,$common) {
	var URL = $bb.baseUrl("department")+"followup/service";
	var obj=$bb.getPageParams();
	var userId=obj.babyId;
	var type=obj.type;//历史记录传参；
	var patientSchemeId=obj.patientSchemeId;
	var answerId = obj.answerId, result;
	var count =1, pagesize =15;
	var HashMap=$common.hashmap(),hashMap=new HashMap();
	$common.date();
	getBabyInfo();

	$(".mask, .AddInformation").hide();
	$("#historyData").click(function (event) {
		getBabyList(1, pagesize);
		var p = new Paging();
		$("#pageTool").html("");
		p.init({target:'#pageTool',pagesize:pagesize,count:count,toolbar:false,callback:function(page,size,count){
			getBabyList(page,size);
		}});
		$(".mask, .AddInformation").show();
	});
	$(".AddInformation h4 span").click(function (event) {
		$(".mask, .AddInformation").hide();
	});
	getBabyInfo();
	if(answerId != null) {
		result = JSON.parse(localStorage.getItem("chineseMedicine"));
		getBabyClinic42(result);
	}

	$("#submitData").click(function(){
		var hashMap = new HashMap();
		hashMap.put("weightVal", $("#weightVal").val());
		hashMap.put("heightVal", $("#heightVal").val());
		hashMap.put("headCircumferenceVal", $("#headCircumferenceVal").val());
		hashMap.put("previousHistory", $("#previousHistory").val());
		hashMap.put("diseaseName", $("#diseaseName").val());
		hashMap.put("shape", $("#shape").val());
		hashMap.put("face", $("#face").val());
		hashMap.put("oral", $("#oral").val());
		hashMap.put("tongue", $("#tongue").val());
		hashMap.put("coatedTongue", $("#coatedTongue").val());
		hashMap.put("pharynx", $("#pharynx").val());
		hashMap.put("fingerprint", $("#fingerprint").val());

		hashMap.put("gravidity", $("input[name='gravidity']:checked").val());
		hashMap.put("feeding", $("input[name='feeding']:checked").val());
		hashMap.put("lungSound", $("input[name='lungSound']:checked").val());
		hashMap.put("diet", $("input[name='diet']:checked").val());
		hashMap.put("addicted", $("input[name='addicted']:checked").val());
		hashMap.put("spirit", $("input[name='spirit']:checked").val());
		hashMap.put("modern", $("input[name='modern']:checked").val());
		hashMap.put("sweating", $("input[name='sweating']:checked").val());
		hashMap.put("sleep", $("input[name='sleep']:checked").val());
		hashMap.put("feces", $("input[name='feces']:checked").val());
		hashMap.put("urinate", $("input[name='urinate']:checked").val());

		$.ajax({
			type: "POST",
			xhrFields : {withCredentials: true},
			crossDomain : true,
			url: URL+"/comm/question/saveOutpatient",
			async: true,
			data: {
				"babyId" : babyId,
				"type" : "chineseMedicine",
				"answerId":answerId,
				"result" : JSON.stringify(hashMap)
			},
			success: function(data){
				if(data.success) {
					alert("保存成功");
				} else {
					alert("保存失败");
				}
			},
		});
	});
	function getBabyInfo() {
		$bb.ajax({
			url: URL + "/patient/patientInfoQueryByPtId",
			data: JSON.stringify({
				data: {
					"patientId": userId,
					"ptSchemeId": patientSchemeId
				}
			}),
			success: function(data){
				if(data.success) {
					var babyInfo=data.data.PatientInfo;
					var gender= (babyInfo.gender=="N")? "男":"女";
					var babyData=$common.babyInfoDeal(babyInfo);
					$common.infoInit({"id":"#babyInfoList","data":babyData});

					$("#babyName").val(babyInfo.userName);
					$("#gender").val(gender);
					var time = new Date(babyInfo.birthday).Format("yyyy年MM月dd日");
					$("#babyBirthday").val(time);
					$("#birthWeight").val(babyInfo.weight==undefined?"未知":babyInfo.weight+"(g)");

					$("#userName").html(babyInfo.createAuthor);
					$("#nowTime").html($common.getNowFormatDate());

					if(answerId == null) {
						var babyDay = $common.DateDiff(babyInfo.birthday,  $common.getNowFormatDate());
						$("#babyDay").val(babyDay);
					}
				}
			}
		});
	}

	function getBabyClinic42(result) {
		$("#weightVal").val(result.weightVal);
		$("#heightVal").val(result.heightVal);
		$("#headCircumferenceVal").val(result.headCircumferenceVal);
		$("#previousHistory").val(result.previousHistory);
		$("#diseaseName").val(result.diseaseName);
		$("#shape").val(result.shape);
		$("#face").val(result.face);
		$("#oral").val(result.oral);
		$("#tongue").val(result.tongue);
		$("#coatedTongue").val(result.coatedTongue);
		$("#pharynx").val(result.pharynx);
		$("#fingerprint").val(result.fingerprint);

		$("input[type=radio][name=gravidity][value='"+result.gravidity+"']").attr("checked",true);
		$("input[type=radio][name=feeding][value='"+result.feeding+"']").attr("checked",true);
		$("input[type=radio][name=lungSound][value='"+result.lungSound+"']").attr("checked",true);
		$("input[type=radio][name=diet][value='"+result.diet+"']").attr("checked",true);
		$("input[type=radio][name=addicted][value='"+result.addicted+"']").attr("checked",true);
		$("input[type=radio][name=spirit][value='"+result.spirit+"']").attr("checked",true);
		$("input[type=radio][name=modern][value='"+result.modern+"']").attr("checked",true);
		$("input[type=radio][name=sweating][value='"+result.sweating+"']").attr("checked",true);
		$("input[type=radio][name=sleep][value='"+result.sleep+"']").attr("checked",true);
		$("input[type=radio][name=feces][value='"+result.feces+"']").attr("checked",true);
		$("input[type=radio][name=urinate][value='"+result.urinate+"']").attr("checked",true);
	}


	//历史记录列表点击：
	$("#dataList").on('click',"ul[result]",function () {
		localStorage.setItem("chineseMedicine",hashMap.get($(this).attr("result")));
		var obj={
			"answerId":$(this).attr("result"),
			"babyId":babyId,
			"type":type
		};
		$bb.openPage("chineseMedicineReport",obj);
	});
	//历史记录列表：
	function getBabyList(page, rows) {
		$.ajax({
			type: "POST",
			xhrFields: {withCredentials: true},
			crossDomain: true,
			async: false,
			url: URL + "/comm/question/getHistoryScore",
			data: {
				babyId: babyId,
				type: type,
				page: page,
				rows: rows
			},
			success: function (data) {
				//console.log(data);
				if (data.success) {
					hashMap = new HashMap();
					count = data.total;
					var str = '';
					for (var i = 0; i < data.rows.length; i++) {
						hashMap.put(data.rows[i].answerId, data.rows[i].result);
						var pretermBirth = 37 * 7 > data.rows[i].pregnancyWeek ? "是" : "否";
						var nowDate =$common.getNowFormatDate();
						var monthAge = $common.DateDiff(nowDate, data.rows[i].birthday) / 30;
						var time = new Date(data.rows[i].createTime).Format("yyyy-MM-dd");
						str += '<ul class="clearfix" result="' + data.rows[i].answerId + '">';
						str += '<li>' + data.rows[i].babyName + '</li>';
						str += '<li>' + data.rows[i].birthWeight + '(g)</li>';
						str += '<li>' + data.rows[i].birthday + '</li>';
						str += '<li>' + data.rows[i].pregnancyWeek + '(天)</li>';
						str += '<li>' + pretermBirth + '</li>';
						str += '<li>' + data.rows[i].doctorName + '</li>';
						str += '<li>' + time + '</li>';
						str += '</ul>';
					}
					$("#dataList").html(str);
				}
			}
		});
	}

});